package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.ClubUpgradePrize;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class ClubUpgradePrizeService {
	@Autowired
	protected GeliDao geliDao;
	
	@Autowired
	protected ClubLevelService clubLevelService;
	
	public List<ClubUpgradePrize> list(boolean isAll) {
		SqlBuilder sqlBuilder = new SqlBuilder();
		sqlBuilder.appendSql("SELECT club_upgrade_prize_id FROM ac_club_upgrade_prize");
		if(!isAll){
			sqlBuilder.appendSql(" where prize !='无'");
		}
		 sqlBuilder.appendSql(" ORDER BY level_id, club_upgrade_prize_id");
		return geliDao.list(ClubUpgradePrize.class, sqlBuilder.getSql());
	}
	
	public List<ClubUpgradePrize> getByLevelId(long levelId) {
		SqlBuilder sqlBuilder = new SqlBuilder();
		sqlBuilder.appendSql("SELECT club_upgrade_prize_id FROM ac_club_upgrade_prize ");
		sqlBuilder.appendSql("WHERE level_id = ");
		sqlBuilder.appendValue(levelId);
		sqlBuilder.appendSql(" ORDER BY level_id, club_upgrade_prize_id");
		
		return geliDao.list(ClubUpgradePrize.class, sqlBuilder.getSql(), sqlBuilder.getValues());
	}
	
	/**
	 * 等级奖品映射
	 * @return
	 */
	public Map<Long, List<ClubUpgradePrize>> getMap(boolean isAll) {
		Map<Long, List<ClubUpgradePrize>> map = new LinkedHashMap<Long, List<ClubUpgradePrize>>();
		List<ClubUpgradePrize> list = list(isAll);
		if (list.isEmpty()) {
			return map;
		}
		
		Set<Long> levelIds = new LinkedHashSet<Long>();
		for (ClubUpgradePrize c : list) {
			levelIds.add(c.getLevelId());
		}
		
		Iterator<Long> i = levelIds.iterator();
		while (i.hasNext()) {
			Long id = i.next();
			List<ClubUpgradePrize> temp = new ArrayList<ClubUpgradePrize>();
			for (ClubUpgradePrize c : list) {
				if (id == c.getLevelId()) {
					c.setClubLevelName(clubLevelService.getClubLevelNameById(c.getLevelId()));
					temp.add(c);
				}
			}
			map.put(id, temp);
		}
		return map;
	}
	
	public List<ClubUpgradePrize> getPrizesByLevelId(boolean isAll,long levelId) {
		return getMap(isAll).get(levelId);
	}
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public long create(Object o){
		return geliDao.create(o);
	}	
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void updateClubUpgradePrizes(List<ClubUpgradePrize> list) {
		for (ClubUpgradePrize c : list) {
			geliDao.update(c);
		}
	}
	
	/**
	 * 添加车友会等级后自动添加
         * @param levelId
         * @return  
	 */
	public long autoInitPrize(long levelId) {
		ClubUpgradePrize prize = new ClubUpgradePrize();
		prize.setLevelId(levelId);
		prize.setPrize("无");
		return create(prize);
	}
}
